$(function () {

    //时间戳转换年月日
    function paddZero(n) { //补零函数
      return n > 10 ? n : '0' + n
    }
  
    function formatTime(time) {
      let d = new Date(time)
  
      let y = d.getFullYear()
      let month = paddZero(d.getMonth() + 1)
      let day = paddZero(d.getDate())
  
  
      return `${y}/${month}/${day}`
    }

  // 相关推荐
  const id = localStorage.getItem('id')
  $.ajax({
    method: 'get',
    url: `http://124.223.14.236:8060/index/show?id=${id}`,
    success: function ({ data }) {
      console.log(data.info.catename);
      // console.log(data.prev[0].id);
      // console.log(data.info.content);
      $('.breadcrumbs').html(`
      <span class="text-muted">当前位置：</span>
              <a href="">优点达资讯</a>
              <small>></small> 
              <a href="">${data.info.catename}</a> <small>></small> 
              <span class="text-muted">正文</span>
      `)

      // 标题
      $('.article-title').html(`
      <h1 class="article-title"><a href="">${data.info.title}</a></h1>
      `)

      // 标题描述
      $('.article-meta').html(`
      <li><a href="">${data.info.author}</a> 发布于 ${formatTime(data.info.update_date)}</li>
              <li>分类：<a href="" rel="category tag">${data.info.catename}</a></li>
              <li><span class="post-views">阅读(${data.info.click})</span></li>
              <li>评论(${data.info.default_data})</li>
              <li></li>
      `)
      $('.article-content').html(`
           <p>
           ${data.info.content}
           </p>
           <!-- 文章内容结束 -->
         
           <!-- 版权描述 -->
           <p class="post-copyright">未经允许不得转载：
           <a href="">优点达资讯</a> &raquo; <a href=""></a></p>
           `)
      // 点赞
      $('.article-social').html(`
            <a href="javascript:;" class="action action-like" >
            <i class="glyphicon glyphicon-thumbs-up"></i>赞 (<span>${data.info.lovenum}</span>)
          </a>
            `)

      // 标签信息
      $('.article-tags').html(`
      标签：<a href="" rel="tag">${data.info.tags}</a> 
      `)
      // 上下一篇
      $('.article-nav-prev').html(`
      <span>上一篇</span>
      <a href="" rel="prev">${data.prev[0].title}</a>
    </span>
    <span class="article-nav-next">
      <span>下一篇</span>
      <a href="" rel="next">${data.next[0].title}</a>
    </span>
      `)

      // console.log(data.recommend);
      let newRel = data.recommend.map((item) => {
        let { content, cateid, catename, id, pic, title } = item
        // console.log(content);
        return `
                <li>
                <a target="_blank" href="">
                  <span>
                    <img src="http://124.223.14.236:8060/${pic}" class="thumb" />
                  </span>
                  ${title}
                </a>
              </li>
                `
      }).join('')
      $('#relates').html(newRel)
    }
  })

  //   右侧广告位
  $.ajax({
    method: 'get',
    url: 'http://124.223.14.236:8060/index/index',
    success: function ({ data }) {
      // console.log(data.sideBanner);
      // console.log(data.allLink);
      let rightList = data.sideBanner.map((item) => {
        let { advimgdesc, advimglink, advimgsrc } = item
        return `
                <div class="textwidget">
                <a href="${advimglink}" rel="nofollow" target="_blank" title="阿里云开学季">
                  <img class="g430" style="width:360px;height:133px;" src="http://124.223.14.236:8060/${advimgsrc}" alt="${advimgdesc}" >
                </a>
               
              </div>
                `
      }).join('')
      $('.textwidget').html(rightList)

      //右侧 有情链接   
      let newLianjie = data.allLink.map((item) => {
        // console.log(item);

        let { title, url, img, des } = item

        return `
              <li>
              <a href="${url}">
                <img src="http://124.223.14.236:8060/${img}" alt="">
                <p>${des}</p>
              </a>
            </li>
              `
      }).join('')
      $('#widget').html(newLianjie)
      // 热门标签
      let newBiaoqian = data.allTag.map((item) => {
        return `
            <li><a title="" href="">${item.tagname}</a></li>
            `
      }).join('')
      $('.widget_tags_inner').html(newBiaoqian)
    }
  })

  // 热门文章
  $.ajax({
    method: 'get',
    url: 'http://124.223.14.236:8060/home/index/recommend',
    success: function ({ data }) {
      // console.log(data);
      // console.log(data.list);
      let newrec = data.list.map((item) => {
        let { pic, title, lovenum } = item

        return `
            <li>
            <a target="_blank" href="">
              <span class="thumbnail">
                <img src="http://124.223.14.236:8060/${pic}" class="thumb" />
              </span>
              <span class="text">${title}</span>
              <span class="text-muted post-views">${lovenum}</span>
            </a>
          </li>
            `
      }).join('')
      $('.widget .items-02').html(newrec)
    }
  })
})